﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using Cassandra;

//Must Set conf/cassandra.yaml: http://www.datastax.com/documentation/developer/csharp-driver/1.0/common/drivers/introduction/driverDependencies_r.html
// start_native_transport: true
// AND.... Restart Node

namespace CassyDb
{
    class CassyClient
    {
        private Cluster _cluster;
        public Cluster Cluster
        {
            get { return _cluster; }
            set { _cluster = value; }
        }


        static void Main(string[] args)
        {
            CassyClient client = new CassyClient();
            client.Connect("127.0.0.1");
            client.Close();
        }


        public void Connect(String node)
        {
            Cluster = Cluster.Builder().AddContactPoint(node).Build();

            Session session = Cluster.Connect();

            #region Setup
            //session.Execute("CREATE KEYSPACE simplex WITH replication " + "= {'class':'SimpleStrategy', 'replication_factor':3};");

            //session.Execute("CREATE TABLE simplex.songs (" +
            //                    "id uuid PRIMARY KEY," +
            //                    "title text," +
            //                    "album text," +
            //                    "artist text," +
            //                    "tags set<text>," +
            //                    "data blob" +
            //                    ");");

            //session.Execute("CREATE TABLE simplex.playlists (" +
            //            "id uuid," +
            //            "title text," +
            //            "album text, " +
            //            "artist text," +
            //            "song_id uuid," +
            //            "PRIMARY KEY (id, title, album, artist)" +
            //            ");");

            //session.Execute("INSERT INTO simplex.songs (id, title, album, artist, tags) " +
            //              "VALUES (" +
            //                  "756716f7-2e54-4715-9f00-91dcbea6cf50," +
            //                  "'La Petite Tonkinoise'," +
            //                  "'Bye Bye Blackbird'," +
            //                  "'Joséphine Baker'," +
            //                  "{'jazz', '2013'})" +
            //                  ";");

            //session.Execute("INSERT INTO simplex.playlists (id, song_id, title, album, artist) " +
            //              "VALUES (" +
            //                  "2cc9ccb7-6221-4ccb-8387-f22b6a1b354d," +
            //                  "756716f7-2e54-4715-9f00-91dcbea6cf50," +
            //                  "'La Petite Tonkinoise'," +
            //                  "'Bye Bye Blackbird'," +
            //                  "'Joséphine Baker'" +
            //                  ");"); 
            #endregion

            session.ChangeKeyspace("ChattySessions");

            RowSet chattyResults = session.Execute("SELECT * FROM Users LIMIT 1");

            RowSet results = session.Execute("SELECT * FROM simplex.songs LIMIT 1");

            Console.WriteLine(String.Format("{0, -30}\t{1, -20}\t{2, -20}\t{3, -30}", "title", "album", "artist", "tags"));
            Console.WriteLine("-------------------------------+-----------------------+--------------------+-------------------------------");

            foreach (Row row in results.GetRows())
            {
                var tags = string.Join(", ", row.GetValue<List<string>>("tags"));

                Console.WriteLine
                    (String.Format("{0, -30}\t{1, -20}\t{2, -20}\t{3}",
                        row.GetValue<String>("title"), row.GetValue<String>("album"),
                        row.GetValue<String>("artist"), tags)
                    );
            }

        }


        public void Close()
        {
            Cluster.Shutdown();
        }
    }
}